import router from './router'
import store from './store'
// 导入跳转进度条
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
const whiteList = ['/login', '/404']
import getPageTitle from '@/utils/get-page-title'
import { asyncRoutes } from '@/router/index'
router.beforeEach(async(to, from, next) => {
  // 路由跳转 加载进度条
  document.title = getPageTitle(to.meta.title)
  NProgress.start()
  const token = store.state.user.token
  const userInfo = store.state.user.userInfo
  if (token) { // 有token
    if (to.path === '/login') { // 去登录
      // 不是正常放行 手动结束
      NProgress.done()
      next('/') // 返回主页
    } else { // 不去登录
      // 页面跳转之前获取用户信息
      if (!userInfo.userId) { // 没有userId 才获取用户信息
        await store.dispatch('user/getUserInfo')
        const newRouterList = asyncRoutes.filter(ele => {
          return store.state.user.userInfo.roles.menus.includes(ele.children[0].name)
        })
        newRouterList.push({ path: '*', redirect: '/404', hidden: true })
        // console.log(newRouterList)
        router.addRoutes([...newRouterList])
        store.commit('menu/setMenuList', [...newRouterList])
        next({
          ...to,
          replace: true
        })
      }
      next() // 放行
    }
  } else { // 没有token
    if (whiteList.includes(to.path)) { // 去白名单
      next() // 放行
    } else { // 不去白名单
      // 不是正常放行 手动结束
      NProgress.done()
      next('/login') // 去登录
    }
  }
})
router.afterEach((to, from) => {
  // 跳转完成 结束进度条
  NProgress.done()
})
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
